
from maya.OpenMaya import *

import random
import Vehicle

reload(Vehicle)

class Worker(Vehicle.Vehicle):
    token = None
    
    isWander = True
    neighbors = []
    
    def __init__( self, _pd ):
        self.token = _pd.allocateToken( self )
        
        Vehicle.Vehicle.__init__( self )
        
        self.reset()
        
    def reset( self ):
        Vehicle.Vehicle.reset( self )
        
        self.maxSpeed = 300.0
        self.maxForce = 600.0
        
        self.speed = 200.0
        self.radius = 25
        
        self.randomizeHeadingOnZXPlane()
        self.token.updateForNewPosition( self.position )
        
    def update( self, _currentTime, _elapsedTime ):
        self.applySteeringForce( self.determineCombinedSteering( _elapsedTime ), _elapsedTime )
        self.token.updateForNewPosition( self.position )
        
    def determineCombinedSteering( self, _elapsedTime ):
        _steeringForce = self.forward
        
        _leakThrough = 0.1
        _obstacleAvoidance = MVector()
        
        if _leakThrough < random.uniform( 0, 1 ):
            _oTime = 6.0
            #_obstacleAvoidance = self.SteerToAvoidObstacles( _oTime, )
            
        if _obstacleAvoidance != MVector.zero:
            _steeringForce += _obstacleAvoidance
        else:
            _collisionAvoidance = MVector()
            _caLeadTime = 3.0
            
            _maxRadius = _caLeadTime * self.maxSpeed * 2.0
            Worker.neighbors = []
            self.token.findNeighbors( self.position, _maxRadius, Worker.neighbors )
            
            if _leakThrough < random.uniform( 0, 1 ):
                print len(Worker.neighbors)
                _collisionAvoidance = self.steerToAvoidNeighbors( _caLeadTime, Worker.neighbors ) * 10
                
            if _collisionAvoidance != MVector.zero:
                _steeringForce += _collisionAvoidance
            else:
                if Worker.isWander:
                    _steeringForce += self.steerForWander( _elapsedTime )
                    
        _steeringForce.y = 0.0
        return _steeringForce